import re
import tkinter as tk
from datetime import datetime
from tkinter import filedialog, simpledialog, ttk
from tkinter import messagebox


#查找文件小窗口
def file_path_query(text):
    window = tk.Tk()
    window.withdraw()
    window.title("请选择目标文件")
    file_path=filedialog.askopenfilename(title=text)#选择目标文件
    window.destroy()
    return file_path

#提示窗体
def show_warning(msg):
    top = tk.Tk()
    top.withdraw()
    top.update()
    tk.messagebox.showwarning("提示", msg)
    print(msg)
    top.destroy()



#选择文件夹窗体
def select_folder():
    window = tk.Tk()
    window.withdraw()
    window.title("请选择存放目标文件夹")
    folder_path = filedialog.askdirectory()
    window.destroy()
    return folder_path

#输入文件名窗体
def input_file_name(name):
    window = tk.Tk()
    window.withdraw()
    window.geometry("300x200")
    file_name = simpledialog.askstring("请输入", name)
    window.destroy()
    return file_name

#输入时间窗体
def input_time(msg):
    window = tk.Tk()
    window.withdraw()
    window.geometry("300x200")
    time = simpledialog.askstring("请输入", msg)
    try:
        # 尝试将字符串解析为日期
        datetime.strptime(time, '%Y-%m-%d')
        return time
    except ValueError:
        # 如果格式不正确，返回 None
        show_warning("请输入正确的日期格式（例如：2023-07-31）")
        return None
    finally:
        window.destroy()

#创建表格窗体
# 创建表格的方法
def create_table(parent, columns, data):
    # 创建表格
    tree = ttk.Treeview(parent, columns=columns, show='headings')

    # 设置列标题
    for col in columns:
        tree.heading(col, text=col)
        tree.column(col, anchor=tk.CENTER)

    # 添加数据
    for item in data:
        tree.insert('', tk.END, values=item)

    # 返回表格组件
    return tree
